//
//  LXCarStockDetailsViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/8.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXCarStockDetailsViewController.h"
#import "LXCarStockTopView.h"
#import "LXCarStockImagView.h"
#import "LXCarStockBottomView.h"
#import "LXCarLeaseApplyViewController.h"
#import "LXCarSellApplyViewController.h"
#import "LXSignRecordViewController.h"

@interface LXCarStockDetailsViewController ()
@property(nonatomic) UIScrollView *scrollView;
@end

@implementation LXCarStockDetailsViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self layoutCustomNav:self.view title:@"车型详情"];
    [self layoutUI];
    
}

-(void)layoutUI{
    
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetCarTypeDetail] params:@{@"id":self.ID} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            
//            [self.scrollView removeAllSubviews];
            
            CheLiangModel *model = [CheLiangModel mj_objectWithKeyValues:resonseObj];
            
            UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, ScreenHeight - kViewTopHeight)];
            [self.view addSubview:scrollView];
            if (@available(iOS 11.0, *)) {
                scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            }
            self.scrollView = scrollView;
            
            LXCarStockTopView *topView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXCarStockTopView class]) owner:nil options:nil].firstObject;
            topView.frame = CGRectMake(0, 0, ScreenWidth, 285);
            topView.carBrandLab.text = [NSString stringWithFormat:@"%@ %@",model.brand, model.model];
            // 租车
            if (model.type == 1) {
                topView.carPriceLab.text = [NSString stringWithFormat:@"%@元起/月",model.floorPrice];
                [topView.carPriceLab addAttributes:TextAttrCorlor([UIColor colorWithHexString:@"#FF921C"]) forText:model.floorPrice size:[UIFont systemFontOfSize:24 weight:UIFontWeightMedium]];

                
                NSString *stock = [NSString stringWithFormat:@"%@辆",model.carNum];
                topView.carStockLab.text = [NSString stringWithFormat:@"库存 %@",stock];
                [topView.carStockLab addAttributes:TextAttrCorlor([UIColor colorWithHexString:@"#FF921C"]) forString:stock];
            }
            // 售车
            else {
                
                NSString *price = [NSString stringWithNull:model.floorPrice];
                topView.carPriceLab.text = [NSString stringWithFormat:@"%@万元起/首付款",price];
                [topView.carPriceLab addAttributes:TextAttrCorlor([UIColor colorWithHexString:@"#FF921C"]) forString:price size:24];
                
                NSString *stock = [NSString stringWithFormat:@"%@辆",model.carNum];
                NSString *qi = [NSString stringWithFormat:@"%@期",model.numberInstallments];
                topView.carStockLab.text = [NSString stringWithFormat:@"库存 %@ 可分期 %@",stock,qi];
                [topView.carStockLab addAttributesArray:@[TextAttrCorlor([UIColor colorWithHexString:@"#FF921C"]),TextAttrCorlor([UIColor colorWithHexString:@"#FF921C"])] forStringArray:@[stock,qi] size:14];
                
                
                
            }
            
            topView.carEnergyLab.text = [Tool getId:[NSString stringWithFormat:@"%@",@(model.energeType)] DicDataWithKey:k_NYLX];
            
            if (model.energeType == 1) {
                topView.carEnduranceLab.text = @"-";
            }
            else {
                topView.carEnduranceLab.text = [NSString stringWithFormat:@"%@ km",model.endurance];
            }
            topView.carPlatformLab.text = [Tool getId:[NSString stringWithFormat:@"%@",model.runPlatfom] DicDataWithKey:k_YXPT];
            topView.catTypeLab.text = [Tool getId:[NSString stringWithFormat:@"%@",model.carType] DicDataWithKey:k_CLLX];

            
            
            [scrollView addSubview:topView];
            
            
            NSMutableArray *imageArys = [NSMutableArray array];
            
            
            NSData *data = [model.image dataUsingEncoding:NSUTF8StringEncoding];

            NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            for (NSDictionary *dic in jsonObject) {
                [imageArys addObject:[NSString stringWithFormat:@"%@",dic[@"path"]]];
            }
            
    

            CGFloat height = [LXCarStockImagView getCellHeightWithDataAry:imageArys];

            
            LXCarStockImagView *imgView = [[LXCarStockImagView alloc] initWithFrame:CGRectMake(0, topView.Y_height + 20, ScreenWidth, height + 50)];
            imgView.imageArys = imageArys;
            [scrollView addSubview:imgView];
            
            
            LXCarStockBottomView *bottomView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXCarStockBottomView class]) owner:nil options:nil].firstObject;
            bottomView.frame = CGRectMake(0, imgView.Y_height, ScreenWidth, 200);
            [scrollView addSubview:bottomView];
            
            
            
            // 租车
            if (model.type == 1) {
                bottomView.contactLab.text = @"出租联系人";
                bottomView.contactPhoneLab.text = [NSString stringWithFormat:@"%@ %@",model.contactName, model.contactPhone];
            }
            else {
                bottomView.contactLab.text = @"";
                bottomView.contactPhoneLab.text = @"";
                bottomView.height = 150;
                bottomView.contactConstraint.constant = -10;
            }
            
            bottomView.remarkLab.text = StringIsEmpty(model.remark)?@"无":model.remark;
            bottomView.height += [NSString remarksHeight:bottomView.remarkLab.text];

           
            
            scrollView.contentSize = CGSizeMake(ScreenWidth, bottomView.Y_height + 30);
            
            
            // 车辆删除
            [topView.carDelBtn whenTapped:^{
                
                [Tool showAlertTitle:@"温馨提示" msg:@"您确定要删除该车型吗?" cancelTitle:@"取消" commitBtnTitle:@"删除" cancelHandlerBlock:^{} commitHandlerBlock:^{
                    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:POST_PostEditCarDelete] params:@{@"id":model.ID} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
                        if (isSuccess) {
                            showAlert(@"删除成功");
                            kUserDefaultSetBool(YES, kRefreshStr);
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                    } failure:^(NSUInteger code, NSString * _Nonnull message) {
                        
                    } withShowHud:YES];
                } onVC:self];
               
            }];
            // 车辆编辑
            [topView.carEditBtn whenTapped:^{
                
                if (model.type == 1) {
                    LXCarLeaseApplyViewController *vc = [[LXCarLeaseApplyViewController alloc] init];
                    CheLiangModel *mm = model;
                    mm.carBrand = model.brand;
                    mm.carMode = model.model;
                    mm.imgs = imageArys;
                    vc.carModel = mm;
                    vc.fromVc = @"LXCarStockViewController";
                    vc.type = 2;
                    [self pushToNextController:vc];
                }
                else {
                    LXCarSellApplyViewController *vc = [[LXCarSellApplyViewController alloc] init];
                    CheLiangModel *mm = model;
                    mm.carBrand = model.brand;
                    mm.carMode = model.model;
                    mm.imgs = imageArys;
                    vc.carModel = mm;
                    vc.fromVc = @"LXCarStockViewController";
                    vc.type = 2;
                    [self pushToNextController:vc];
                }
                
              
            }];
            
            
            [bottomView.singView whenTapped:^{
                            
                LXSignRecordViewController *vc = [[LXSignRecordViewController alloc] init];
                [self pushToNextController:vc];
            }];
            
    
            
        }
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        
    } withShowHud:YES];

}

@end
